<template>
  <el-dialog width="470px" class="app-dialog" title="修改密码" :visible.sync="visible" :before-close="handleClose" :close-on-click-modal="false">
    <el-form ref="form" :model="model" :rules="rules">
      <!-- 原密码 -->
      <el-form-item label="原密码" prop="currentPassword">
        <el-input ref="currentPassword" v-model="model.currentPassword" type="text" name="password" auto-complete="off" placeholder="请输入" />
      </el-form-item>

      <!-- 新密码 -->
      <el-form-item label="新密码" prop="newPassword">
        <el-input
          ref="newPassword"
          v-model="model.newPassword"
          :type="passwordType['newPassword']"
          placeholder="6-20位数字、字母、符号"
          name="newPassword"
        />
        <span class="show-pwd" @click="handlePassword('newPassword')">
          <svg-icon :icon-class="passwordType['newPassword'] === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-form-item label="重复密码" prop="checkNewPassword">
        <el-input
          ref="checkNewPassword"
          v-model="model.checkNewPassword"
          :type="passwordType['checkNewPassword']"
          placeholder="请输入"
          name="checkNewPassword"
        />
        <span class="show-pwd" @click="handlePassword('checkNewPassword')">
          <svg-icon :icon-class="passwordType['checkNewPassword'] === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleSubmit">保 存</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { mapGetters } from 'vuex'
import { changePassword } from '@/api/user'
export default {
  name: 'ResetPwd',
  data: function () {
    return {
      model: {},
      rules: {
        currentPassword: [{ required: true, trigger: 'change', message: '请输入您的密码' }],
        newPassword: [{ required: true, trigger: 'change', message: '请输入您的新密码' }],
        checkNewPassword: [
          { required: true, trigger: 'change', message: '请再次输入您的密码' },
          { required: true, validator: this.checkNewPasswordValid, trigger: 'change' },
        ],
      },
      // 密码脱敏
      passwordType: {
        newPassword: 'password',
        checkNewPassword: 'password',
      },
    }
  },
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
  },
  computed: {
    ...mapGetters(['sidebar', 'avatar', 'device', 'userName']),
  },
  methods: {
    handleClose() {
      this.$refs.form.resetFields()
      this.$emit('close', false)
    },
    handlePassword(str) {
      this.passwordType[str] = this.passwordType[str] === 'password' ? '' : 'password'
      this.$nextTick(() => this.$refs[`${str}`].focus())
    },
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.userName == 'admin1') {
            this.$message({
              message: '管理员账号不可修改',
              type: 'error',
            })
          } else {
            changePassword(this.model).then(res => {
              this.$message({
                message: '密码修改成功',
                type: 'success',
              })
              this.handleClose()
            })
          }
        }
      })
    },

    newPasswordValid(rule, value, callback) {
      const reg = /^(?=.*[A-Za-z])(?=.*\d)(?=.*[!@#$%^&*()_+~`|}{[\]:;?><,./-])[A-Za-z\d!@#$%^&*()_+~`|}{[\]:;?><,./-]{6,20}$/
      reg.test(value) ? callback() : callback(new Error('密码长度不少于6个字符不超过20个字符，必须包含数字、字母、符号'))
    },
    checkNewPasswordValid(rule, value, callback) {
      this.model.newPassword !== value ? callback(new Error('两次输入的密码不一致')) : callback()
    },
  },
}
</script>

<style lang="scss" scoped>
.show-pwd {
  position: absolute;
  right: 10px;
  top: 50%;
  font-size: 12px;
  color: #31303f;
  cursor: pointer;
  user-select: none;
}
::v-deep .el-form-item__label {
  font-size: 14px;
  color: #333333;
  font-weight: initial;
}
::v-deep .is-required .el-form-item__label {
  position: relative;
  &:before {
    display: none;
    content: '';
  }
  &:after {
    content: '*';
    color: #f56c6c;
    position: absolute;
    right: 0;
    top: 0;
  }
}
</style>
